/**
 * Created with IntelliJ IDEA.
 * User: teddy zhang
 * Date: 2015-04-17
 * Time: 11:25
 */

define(function (require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    require('lib/check');
    //require('page/css/register.css');

    $.widget('rk.usersettingPassportCtrl', {
        options: {
            selector:null
        },

        _create: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._render();

        },

        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

        },


        _bind: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var input_passport_phone = $("#input_passport_phone");
            var input_passport_phoneMessageLabel = $("#input_passport_phoneMessageLabel");
            var phone_captchaMessageLabel = $("#phone_captchaMessageLabel");
            var phone_validate_save_button = $("#phone_validate_save_button");
            var phone_validate_saving_button = $("#phone_validate_saving_button");
            var phone_captcha = $("#phone_captcha");
            var phone_captcha_button = $("#phone_captcha_button");


            var personalEmail_captcha_button = $("#personalEmail_captcha_button");
            var personalEmail_captcha = $("#personalEmail_captcha");
            var personalEmail_captchaMessageLabel = $("#personalEmail_captchaMessageLabel");
            var input_passport_personalEmail = $("#input_passport_personalEmail");
            var input_passport_personalEmailMessageLabel = $("#input_passport_personalEmailMessageLabel");
            var personalEmail_validate_save_button = $("#personalEmail_validate_save_button");
            var personalEmail_validate_saving_button = $("#personalEmail_validate_saving_button");

            var second = 0;
            $("#personalEmail_captcha").initCheck();
            $("#input_passport_personalEmail").initCheck();
            $("#input_passport_phone").initCheck();
            $("#phone_captcha").initCheck();



            $("#phone_check_button").toggle(function () {
                $("#phone_arrow_id").removeClass("arrow_down").addClass("arrow_up");
                $("#phone_verify_frame").slideDown();
            }, function () {4
                $("#phone_arrow_id").removeClass("arrow_up").addClass("arrow_down");
                $("#phone_verify_frame").slideUp();
            });

            $("#personalEmail_check_button").toggle(function () {
                $("#personalEmail_arrow_id").removeClass("arrow_down").addClass("arrow_up");
                $("#personalEmail_verify_frame").slideDown();
            }, function () {
                $("#personalEmail_arrow_id").removeClass("arrow_up").addClass("arrow_down");
                $("#personalEmail_verify_frame").slideUp();
            });





            $(elem).on('click', '#personalEmail_captcha_button', function (event) {
                event.preventDefault();
                if (second > 0) {
                    return;
                }

                var login_Name = $.trim($('#personalEmail_verify_frame input[name="loginName"]').val());
                if(!input_passport_personalEmail.check()) return;
                $.postJson('/json/oa_profile/passport-captcha-fetch.action',{loginName:login_Name}).done(function (json) {
                        var status = json.status;
                        switch (status) {
                            case 400:
                                input_passport_personalEmailMessageLabel.html(rk.i18n('USERSET_USER_EXIST'));
                                input_passport_personalEmailMessageLabel.show();
                                input_passport_personalEmail.removeClass("input_error").addClass("input_error");
                                break;
                            case 0:
                                rk.noticeSuccess(rk.i18n('USERSET_CODE_SENT'));
                                second = 60;
                                email_countDown();
                                break;
                            case 501:
                                rk.noticeError(rk.i18n('USERSET_CODE_SENT_TRY_AFTER_60S'));
                                personalEmail_validate_save_button.show();
                                personalEmail_validate_saving_button.hide();
                                break;
                            default:
                                rk.noticeError(rk.i18n('REQUEST_ERROR'));
                                second = 60;
                                email_countDown();
                        }
                });
                return false;
            });



            $(elem).on('click', 'div.register_help a', function (event) {
                event.preventDefault();
                $('#passport_help').html(rk.templateText(require('page/tmpl/usersetting/tpl_user_setting_help.tpl')));
                rk.popup({
                    name: "successfulPopup",
                    width: 580,
                    success: function(popupSelector) {
                        var win = $(popupSelector);
                        win.on('click','a.pop_close,a[type="pop_close_button"]',function(){
                            rk.closePopup();
                        });
                    },
                    target: 'passport_help'
                });

                return false;
            });


            $(elem).on('click', '#phone_captcha_button', function (event) {
                event.preventDefault();
                if (second > 0) {
                    return;
                }

                var login_Name = $.trim($('#phone_verify_frame input[name="loginName"]').val());
                if(!input_passport_phone.check()) return;
                $.postJson('/json/oa_profile/passport-captcha-fetch.action',{loginName:login_Name}).done(function (json) {
                    var status = json.status;
                    switch (status) {
                        case 400:
                            input_passport_phoneMessageLabel.html(rk.i18n('USERSET_USER_EXIST'));
                            input_passport_phoneMessageLabel.show();
                            input_passport_phone.removeClass("input_error").addClass("input_error");
                            break;
                        case 0:
                            rk.noticeSuccess(rk.i18n('USERSET_CODE_SENT'));
                            second = 60;
                            phone_countDown();
                            break;
                        case 501:
                            rk.noticeError(rk.i18n('USERSET_CODE_SENT_TRY_AFTER_60S'));
                            phone_validate_save_button.show();
                            phone_validate_saving_button.hide();
                            break;
                        default:
                            rk.noticeError(rk.i18n('REQUEST_ERROR'));
                            second = 60;
                            phone_countDown();
                    }
                });
                return false;
            });





            $(elem).on('click', '#personalEmail_validate_save_button', function (event) {
                event.preventDefault();
                if (!input_passport_personalEmail.check() && personalEmail_captcha.check()) {
                    return;
                }

                personalEmail_validate_save_button.hide();
                personalEmail_validate_saving_button.show();

                var login_Name = $.trim($('#personalEmail_verify_frame input[name="loginName"]').val());
                var _captcha = $.trim(personalEmail_captcha.val());
                $.postJson('/json/oa_profile/passport-captcha-check.action',{loginName:login_Name,captcha:_captcha}).done(function (json) {
                        var status = json.status;
                        switch (status) {
                            case 400:
                                input_passport_personalEmailMessageLabel.html(rk.i18n('USERSET_USER_EXIST'));
                                input_passport_personalEmailMessageLabel.show();
                                personalEmail_validate_save_button.show();
                                personalEmail_validate_saving_button.hide();
                                input_passport_personalEmail.removeClass("input_error").addClass("input_error");
                                break;
                            case 409:
                                personalEmail_captchaMessageLabel.html(rk.i18n('USERSET_BAD_CODE'));
                                personalEmail_captchaMessageLabel.show();
                                personalEmail_validate_save_button.show();
                                personalEmail_validate_saving_button.hide();
                                personalEmail_captcha.removeClass("input_error").addClass("input_error");
                                break;
                            case 0:
                                $('#email_update_success_pop').html(rk.templateText(require('page/tmpl/usersetting/tpl_user_setting_passport_success_email.tpl')));
                                 rk.popup({
                                    name: "successfulPopup",
                                    width: 580,
                                    success: function(popupSelector) {
                                         var win = $(popupSelector);
                                        win.on('click','a.pop_close,a[type="pop_close_button"]',function(){
                                            rk.closePopup();
                                        });
                                     },
                                    target: 'email_update_success_pop',
                                     afterClose: function() {
                                         opt.selector.trigger('click');
                                     }
                                 });

                                break;
                            default:
                                rk.noticeError(rk.i18n('REQUEST_ERROR'));
                                personalEmail_validate_save_button.show();
                                personalEmail_validate_saving_button.hide();
                        }

                }).fail(function (def, code, message) {
                    def.status != 0 && rk.noticeError(rk.i18n('REQUEST_ERROR'));
                });


                return false;
            });


            $(elem).on('click', '#phone_validate_save_button', function (event) {
                event.preventDefault();
                if (!input_passport_phone.check() && phone_captcha.check()) {
                    return;
                }
                phone_validate_save_button.hide();
                phone_validate_saving_button.show();
                var login_Name = $.trim($('#phone_verify_frame input[name="loginName"]').val());
                var _captcha = $.trim(phone_captcha.val());
                $.postJson('/json/oa_profile/passport-captcha-check.action',{loginName:login_Name,captcha:_captcha}).done(function (json) {
                    var status = json.status;
                    switch (status) {
                        case 400:
                            input_passport_phoneMessageLabel.html(rk.i18n('USERSET_USER_EXIST'));
                            input_passport_phoneMessageLabel.show();
                            phone_validate_save_button.show();
                            phone_validate_saving_button.hide();
                            input_passport_phone.removeClass("input_error").addClass("input_error");
                            break;
                        case 409:
                            phone_captchaMessageLabel.html(rk.i18n('USERSET_BAD_CODE'));
                            phone_captchaMessageLabel.show();
                            phone_validate_save_button.show();
                            phone_validate_saving_button.hide();
                            phone_captcha.removeClass("input_error").addClass("input_error");
                            break;
                        case 0:
                            $('#phone_update_success_pop').html(rk.templateText(require('page/tmpl/usersetting/tpl_user_setting_passport_success_phone.tpl')));
                            rk.popup({
                                name: "successfulPopup",
                                width: 580,
                                target: 'phone_update_success_pop',
                                success: function(popupSelector) {
                                    var win = $(popupSelector);
                                    win.on('click','a.pop_close,a[type="pop_close_button"]',function(){
                                        rk.closePopup();
                                    });
                                },
                                afterClose: function() {
                                    opt.selector.trigger('click');
                                }
                            });
                            break;
                        default:
                            rk.noticeError(rk.i18n('REQUEST_ERROR'));
                            phone_validate_save_button.show();
                            phone_validate_saving_button.hide();
                    }

                }).fail(function (def, code, message) {
                    def.status != 0 && rk.noticeError(rk.i18n('REQUEST_ERROR'));
                });
                return false;
            });



            function email_countDown() {
                if (second > 0) {
                    personalEmail_captcha_button.html(rk.i18n('USERSET_RETRIVE_AFTER_SECONDS', second)).addClass("button_disable");
                    setTimeout(email_countDown, 1000);
                } else {
                    personalEmail_captcha_button.html(rk.i18n('USERSET_RETRIVE_CODE')).removeClass("button_disable");
                }
                second--;
            }



            function phone_countDown() {
                if (second > 0) {
                    phone_captcha_button.html(rk.i18n('USERSET_RETRIVE_AFTER_SECONDS', second)).addClass("button_disable");
                    setTimeout(phone_countDown, 1000);
                } else {
                    phone_captcha_button.html(rk.i18n('USERSET_RETRIVE_CODE')).removeClass("button_disable");
                }
                second--;
            }
        },

            _render: function () {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                var passport_HTML = require('page/tmpl/usersetting/tpl_user_setting_passport.tpl');

                $.postJson('/json/oa_profile-edit/passport.action').done(function (json) {
                    if (json.status == 0) {
                        var data = json.data;
                        $(elem).html(rk.templateText(passport_HTML, data));
                        me._bind();
                    }

                }).fail(function (def, code, message) {
                    def.status != 0 && rk.noticeError(rk.i18n('REQUEST_ERROR'));
                });
            }
    });

    return $;

});

